using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using Wrath.Xml.Objects;
using Wrath.Xml.Objects.Controls;

namespace Wrath.RuleForge.Windowing
{
    public partial class WindowControlComponent : Control
    {
        private Image frameImage;
        private Rectangle frameRectangle;

        FrameComponent frame;

        public WindowControlComponent()
        {
            InitializeComponent();
        }

        public WindowControlComponent(WindowControl definition, List<WindowControl> controls)
        {            
            InitializeComponent();

            if (definition.Position is ControlBounds)
            {
                ControlBounds bounds = (ControlBounds)definition.Position;
                this.Left = bounds.X;
                this.Top = bounds.Y;
                this.Width = bounds.Width;
                this.Height = bounds.Height;
            }
            else if (definition.Position is ControlAnchor)
            {
                ControlAnchor anchor = (ControlAnchor)definition.Position;
                
                // TODO: Anchor positioning!!
            }

            if (definition.Frame != null)
            {
                frame = new FrameComponent(definition.Frame.Name, new Rectangle(0, 0, Width, Height));

                //frameImage = CurrentRuleSet.GetFrameImage(definition.Frame.Name);
                //frameRectangle = new Rectangle();
                //frameRectangle.X = 0;
                //frameRectangle.Y = 0;
                //frameRectangle.Width = this.Width - 1;
                //frameRectangle.Height = this.Height - 1;

                //if (!String.IsNullOrEmpty(definition.Frame.Offset))
                //{
                //    string[] offset = definition.Frame.Offset.Split(',');
                //    if (offset.Length == 4)
                //    {
                //        int left = Convert.ToInt32(offset[0].Trim());
                //        int top = Convert.ToInt32(offset[1].Trim());
                //        int right = Convert.ToInt32(offset[2].Trim());
                //        int bottom = Convert.ToInt32(offset[3].Trim());

                //        frameRectangle.X = left;
                //        frameRectangle.Y = top;
                //        frameRectangle.Width = this.Width - (left + right) - 1;
                //        frameRectangle.Height = this.Height - (top + bottom) - 1;
                //    }
                //}
            }
        }

        protected override void OnPaint(PaintEventArgs pe)
        {
            // Calling the base class OnPaint
            base.OnPaint(pe);

            if (DesignMode)
            {
                // TODO: Add custom paint code here
                pe.Graphics.FillRectangle(Brushes.WhiteSmoke, 0, 0, this.Width - 1, this.Height - 1);
                pe.Graphics.DrawRectangle(Pens.Black, 0, 0, this.Width - 1, this.Height - 1);
            }
            else
            {
                //if (frameImage != null && frameRectangle != null)
                //{
                //    pe.Graphics.DrawImage(frameImage, frameRectangle);
                //}

                if (frame != null)
                {
                    frame.Paint(pe.Graphics);
                }                
            }
        }
    }
}
